package com.jframe.cfg.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jframe.cfg.model.DicType;
import com.jframe.cfg.model.Iframe;
import com.jframe.cfg.model.Tablemapping;
import com.jframe.cfg.service.DictypeService;
import com.jframe.cfg.service.TablemapService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.DicCache;
import com.jframe.sys.controller.utils.JsBuilder;
import com.jframe.sys.controller.utils.QueryHelp;
import com.jframe.sys.controller.utils.TabmCache;
import com.jframe.sys.orm.utils.ResultSet;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Columns;
import com.jframe.sys.webui.Editor;
import com.jframe.sys.webui.Grid;
import com.jframe.sys.webui.Tree;

public class TablemapAction extends ExtendAction{

	private static String ACTION="tablemapAction";
	private static String SERVICE="tablemapService";
	private static Class<Tablemapping> CLAZZ=Tablemapping.class;
	
		public void list(){
			List<Columns> frozenColumns = new ArrayList<Columns>();//固定列
			List<Columns> commonColumns = new ArrayList<Columns>();
			frozenColumns.add(new Columns("ck","",40,""));
			frozenColumns.add(new Columns("enname", "代码", 150,""));
			frozenColumns.add(new Columns("cnname", "名称", 150,""));
			frozenColumns.add(new Columns("tablename","表名",150,""));
			commonColumns.add(new Columns("keyname","键值字段",80,""));
			commonColumns.add(new Columns("valuename","显示字段",80,""));
			commonColumns.add(new Columns("upname","上级字段",80,""));
			commonColumns.add(new Columns("conditions","查询条件",250,""));
			commonColumns.add(new Columns("seqname","排序字段",80,""));
			commonColumns.add(new Columns("sorttype","排序方式",80,""));
			//commonColumns.add(new Columns("typeId","子系统",120,new Editor("combobox",true),DicCache.getComboboxJson("sm_sys_app")));
			Grid grid = new Grid();
			grid.setUrl(ACTION+".do?method=load");
			String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
			this.request.setAttribute("js", js);
			this.forward("/jframe/cfg/list/tablemapList.jsp");
		}
	
		public void load(){
			this.baseLoad(SERVICE, CLAZZ);		
		}
		
		public void save(){
			Map<String,Object> map = new HashMap<String,Object>();
			String obj = this.request.getParameter("obj");
			Tablemapping tablemapping = (Tablemapping) JsonUtil.json2Obj(obj, CLAZZ);
			TablemapService tablemapService = (TablemapService)this.getBean(SERVICE);				
			if(StringUtil.null2Space(tablemapping.getEnname()).indexOf("sm_")!=0){
				map.put("result", "表映射字典代码请用'sm_'开头！");
				this.print(JsonUtil.map2Json(map));
				return;
			}
			Tablemapping mapping =tablemapService.load("enname", tablemapping.getEnname());
			if(!StringUtil.null2Space(mapping.getCnname()).equals("")&&!StringUtil.null2Space(mapping.getCnname()).equals(tablemapping.getCnname())){
				map.put("result", "代码重复，请重新填写！");
				this.print(JsonUtil.map2Json(map));
				return;
			}
			tablemapService.save(tablemapping);
			map.put("result", true);
			map.put("obj", tablemapping);
			this.print(JsonUtil.map2Json(map));
		}
		
		public void delete(){
			this.baseDelete(SERVICE);
		}
		
		public void getObjById(){
			this.print(JsonUtil.obj2Json(this.getObject(SERVICE)));
		}
		
		public void reload(){
			int id = StringUtil.parseInt(request.getParameter("id"));
			TablemapService tablemapService = (TablemapService)this.getBean(SERVICE);		
			this.print(tablemapService.reload(id));
		}
		
		public void getCache(){
			//this.log.debug(TabmCache.getComboboxJson("sm_sys_menu"));
		}
		
		public void getRightTrees(){
			List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
			String json = TabmCache.getCombotreeJson("sm_sys_app");
				List<Tree> ls = JsonUtil.json2List(json, Tree.class);
				String ids = "";
				
				for(Tree tree:ls){
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("id", "root"+tree.getId());
					map.put("name", tree.getText());
					map.put("pId", 0);
					ids +=tree.getId()+",";
					maps.add(map);
				}
				
				if(ls.size()>0){
					ids = ids.substring(0, ids.length()-1);
				}
				
				TablemapService tablemapService = (TablemapService)this.getBean(SERVICE);		
				List<Tablemapping> treels = tablemapService.findListByUpIds(ids);
				for(Tablemapping tablemapping:treels){
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("id", tablemapping.getId());
					map.put("name", tablemapping.getCnname());
					map.put("pId", "root"+tablemapping.getTypeId());			
					maps.add(map);
				}
				this.print(JsonUtil.list2Json(maps));
		}

}
